<?
/**
* Base Object
*
* Base object is an template that is used by all DATA objects in system to inherit a common functionalities 
*
* @autor Stan
* @version 1.0
* @package Local library
*/
class base_data_object
{
  var $template_data = array();
  var $data = array();
  private $myDB;
  
  /**
  * Constructor
  */
  function __construct()
  {
    # make connection
    $this->db_connect();

  }
  
    
    /**
  * db_connect 
  * Function makes connection if none exists yet, if exists return existing connection.
  */
  function db_connect($return_db_obj=false)
  {
    if (!($this->myDB instanceof myDB))
    { 
	    try{
	    	
	    	$mySingletonDb = myDB::singleton(FRM_DB_DSN, $GLOBALS['FRM_DB_OPTIONS'],$GLOBALS['FRM_DB_INIT_STATEMENTS']);
	    	if(frm_error::isError($mySingletonDb))
	    		throw $mySingletonDb;
	    	
	    	$this->myDB = $mySingletonDb;
	    }
	    catch (frm_error $e)
	    {
	    	 $link = $GLOBALS['system_url'].FRM_FATAL_ERROR_REDIR_URL;
	       general::redirect($link);
	    }
    };
    
    if ($return_db_obj)
    	return $this->myDB;
  }
  
  
  /**
  * 
  * checks connection state
  * @return boolean indicating established connection or frm_error
  */
  public function checkConnection()
  {
  	if (empty($this->myDB))
  	  return false;
  	  
  	return true;
  	
  }
  
  
  /**
   * Function sends query to DB and return result or framework error object
   *
   * @param string $sql, query to execute or to prepare
   * @param integer $in_get_mode 
   * @param array $in_data option parameter to pass data to prepared statement
   * @return mixed
   */
  public function db_query($in_sql, $in_get_mode = DB_ALL, $in_data=array())
  {
    $this->db_connect();
    
    if($in_get_mode == DB_ALL)
      $result = $this->myDB->getAll($in_sql,$in_data);
    elseif ($in_get_mode == DB_COL)
      $result = $this->myDB->getCol($in_sql,$in_data);
    elseif ($in_get_mode == DB_ROW)
      $result = $this->myDB->getRow($in_sql,$in_data);
    elseif ($in_get_mode == DB_ONE)
      $result = $this->myDB->getOne($in_sql,$in_data);
    elseif($in_get_mode == DB_ASSOC)
      $result = $this->myDB->getAssoc($in_sql,$in_data);
    elseif($in_get_mode == DB_QUERY)
      $result = $this->myDB->query($in_sql);
    else
      return new frm_error(ERROR_UNEXPECTED, "Uknown query type [$in_get_mode]");
  
    return $result;
  }
  
  
   /**
  * Function call autoExecute on DB object and return result or framework error object
  * 
  * @param mixed $in_table
  * @param mixed $in_data
  * @param mixed $in_mode
  * @param mixed $in_where
  */
  public function db_autoExec($in_table, $in_data, $in_mode = DB_AUTOQUERY_INSERT, $in_where = '', $flag_encode=true)
  {
    $this->db_connect();
    
    $result = $this->myDB->autoExecute($in_table, $in_data, $in_mode, $in_where, $flag_encode);
    return $result;  
  }

  
  /**
   * begins transaction in DB 
   * @return boolean result
   * @return frm_error
   */
  public function db_beginTransaction()
  {
  	try{
  	
	    if(!$this->myDB->beginTransaction())
	      return new frm_error(ERROR_UNEXPECTED, 'transactions not supported');

  	}
  	catch (PDOException $e)
  	{
  		return new frm_error(ERROR_UNEXPECTED, 'begin transaction exception:'.$e->getMessage());
  	}
	      
	      
    return true;
  }
  
  
  public function db_commit()
  {
  	try{
  	
	    if(!$this->myDB->inTransaction())
	      return new frm_error(ERROR_UNEXPECTED, 'commit called not in transaction!');
	      
	    $res = $this->myDB->commit();
  	}
  	catch (PDOException $e)
  	{
  		return new frm_error(ERROR_UNEXPECTED, 'commit transaction exception:'.$e->getMessage());
  	}
   
  }
  
  public function db_rollback()
  {
  	try{
  	
	    if(!$this->myDB->inTransaction())
	      return new frm_error(ERROR_UNEXPECTED, 'rollback called not in transaction!');
	      
	    $res = $this->myDB->rollback();
  	}
  	catch (PDOException $e)
  	{
  		return new frm_error(ERROR_UNEXPECTED, 'rollback transaction exception:'.$e->getMessage());
  	}
   
  }
  
    
  /**
   * function returns autoincrement ID of last inserted record
   * @return int or frm error
   */
  protected function last_insert_id()
  {
  	try{
	    $result = $this->myDB->lastInsertId();
  	}
  	catch (PDOException $e)
  	{
  		return new frm_error(ERROR_UNEXPECTED, 'last insert id exception:'.$e->getMessage());
  	}
      
    return $result;
  }
  
  
  /**
  * check if value is not already in DB
  * 
  * @param string $in_table
  * @param array $in_field
  * @param array $in_value
  * @return boolean 
  */
  public function uniqueValueCheck($in_table, $in_fields, $in_values)
  {
    if(is_array($in_fields) && is_array($in_values))
    {
      $where = '1';
      
      foreach($in_fields as $key => $field)
        $where .= " AND `$in_table`.`$field` = '$in_values[$key]'";
    }
    elseif(!is_array($in_fields) && !is_array($in_values))
    {
      $where = "`$in_table`.`$in_fields` = '$in_values'";
    }
    else
      return new frm_error(ERROR_UNEXPECTED, 'wrong input data format');
      
    $sql = "SELECT 1 FROM `$in_table` WHERE $where";
    
    $res = $this->db_query($sql, DB_ONE);

    if($res == 1)
      return FALSE; // NOT unique
    else
      return TRUE; // unique
  }
  
  
};
?>
